//
//  EditAlarmViewController.m
//  project
//
//  Created by 圆圆陈 on 2018/1/3.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "EditAlarmViewController.h"

@interface EditAlarmViewController ()
@property(nonatomic,strong)UIDatePicker *datePciker;
@property(nonatomic,strong)NSMutableArray *weekBtnArr;
@end

@implementation EditAlarmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _weekBtnArr = [NSMutableArray array];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20),screenWidth, 44)];
    lable.textColor=[UIColor blackColor];
    lable.font=SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = HHH_Text(@"闹钟");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UIButton *rightButton = [[UIButton alloc] init];
    rightButton.frame = CGRectMake(HHHWIDTH-80-10, 20+FitSimpleIpx(0, 20), 80, 44);
    [rightButton setTitleNormal:HHH_Text(@"保存")];
    rightButton.titleLabel.font = SNFONTMEDIUM(16);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton setTitleColorNormal:UIColorHEX(0xFF9500)];
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; // 这里是用大写的 H
    NSString* dateStr = [formatter stringFromDate:[NSDate date]];
    NSDate *date = [formatter dateFromString:dateStr];

    _datePciker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,naView.bottom+2, HHHWIDTH, FitH(150))];
    _datePciker.datePickerMode = UIDatePickerModeTime;
    _datePciker.locale = [NSLocale currentLocale];
    [_datePciker setDate:date];
    [_datePciker addTarget:self action:@selector(valueChange) forControlEvents:UIControlEventValueChanged];
    [_datePciker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
    [self.view addSubview:_datePciker];
    
    NSArray *weekArr = @[HHH_Text(@"星期天"),HHH_Text(@"星期一"),HHH_Text(@"星期二"),HHH_Text(@"星期三"),HHH_Text(@"星期四"),HHH_Text(@"星期五"),HHH_Text(@"星期六")];
    
    for (int i = 0; i <weekArr.count; i++) {
        UIView *bgView = [[UIView alloc] initWithFrame:SNFRAME(0, _datePciker.bottom+20 + i*50, screenWidth, 50)];
        [self.view addSubview:bgView];
        
        UILabel *weekLabel = [[UILabel alloc]initWithFrame:SNFRAME(25, 15, 100, 20)];
        weekLabel.text      = weekArr[i];
        weekLabel.font      = SNFONTMIN(15.0);
        weekLabel.textColor = SNCOLORBLACK;
        [bgView addSubview:weekLabel];
        
        UIButton *weekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        weekBtn.frame = SNFRAME(screenWidth -40-25, 5, 40, 40);
        weekBtn.tag   = 100+i;
        [weekBtn setImageNormal:@"未勾选"];
        [weekBtn setImageSelecte:@"已勾选"];
        [weekBtn addTouchUpTarget:self action:@selector(weekBtnClick:)];
        [bgView addSubview:weekBtn];
        
        UIView *lineV= [[UIView alloc]initWithFrame:SNFRAME(0, 50,screenWidth, 0.5)];
        [bgView addSubview:lineV];
        lineV.backgroundColor = UIColorHEX(0xD9D9DD);
        [_weekBtnArr addObject:weekBtn];
    }
    
    if (_type==1) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"HH:mm"]; // 这里是用大写的 H
        NSString* dateStr = [NSString stringWithFormat:@"%02ld:%02ld",_model.hours,_model.mins];
        NSDate *date = [formatter dateFromString:dateStr];
        [_datePciker setDate:_model.date];
        for (UIButton *weekBtn in _weekBtnArr) {
            switch (weekBtn.tag) {
                case 100:
                    weekBtn.selected = _model.SUN ;
                    break;
                case 101:
                    weekBtn.selected = _model.MON ;
                    break;
                case 102:
                    weekBtn.selected = _model.TUE ;
                    break;
                case 103:
                    weekBtn.selected = _model.WED ;
                    break;
                case 104:
                    weekBtn.selected = _model.THU ;
                    break;
                case 105:
                    weekBtn.selected = _model.FRI ;
                    break;
                case 106:
                    weekBtn.selected = _model.SAT ;
                    break;
                default:
                    break;
            }
        }
    }
    
//    SNShadowView *shadowLine = [[SNShadowView alloc] init];
//    shadowLine.frame = SNFRAME(0,20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
//    [shadowLine showWithType:1];
//    [self.view addSubview:shadowLine];
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
//    [self.view addSubview:shadowLine];
}
-(void)setModel:(SNAlarmModel *)model{
    _model = model;
}
-(void)weekBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        switch (sender.tag) {
            case 100:
                _model.SUN = YES;
                break;
            case 101:
                _model.MON = YES;
                break;
            case 102:
                _model.TUE = YES;
                break;
            case 103:
                _model.WED = YES;
                break;
            case 104:
                _model.THU = YES;
                break;
            case 105:
                _model.FRI = YES;
                break;
            case 106:
                _model.SAT = YES;
                break;
            default:
                break;
        }
    }else{
        switch (sender.tag) {
            case 100:
                _model.SUN = NO;
                break;
            case 101:
                _model.MON = NO;
                break;
            case 102:
                _model.TUE = NO;
                break;
            case 103:
                _model.WED = NO;
                break;
            case 104:
                _model.THU = NO;
                break;
            case 105:
                _model.FRI = NO;
                break;
            case 106:
                _model.SAT = NO;
                break;
            default:
                break;
        }
    }
}
- (void)leftbuttonClick
{
    SN_USER = [SNUserInfoModel getUserInfoFromDBWithUserID:[KK_LastUser getObjectValue]];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightButtonClick{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; // 这里是用大写的 H
    NSDate *currentDate = [NSDate date];
    NSString *currentDateStr = [formatter stringFromDate:currentDate];
    NSString *dayString = [currentDateStr componentsSeparatedByString:@" "][0];
    NSDate *date =_datePciker.date;
    NSString *dateStr = [formatter stringFromDate:date];
    NSString *timeString = [dateStr componentsSeparatedByString:@" "][1];
    NSDate *realDate = [formatter dateFromString:[NSString stringWithFormat:@"%@ %@",dayString,timeString]];
    _model.open = YES;
    _model.hours = realDate.hour;
    _model.mins = realDate.minute;
    _model.Once = YES;
    _model.date = [currentDate compare:realDate]==-1?realDate:[realDate dateAfterDay:1];
    for (UIButton *weekBtn in _weekBtnArr) {
        if (weekBtn.selected) {
            _model.Once = NO;
        }
    }
    if (_type==0) {
        _model.orderIndex = SN_USER.showAlarmArray.count;
        NSLog(@"_model====%@",_model);
        [SN_USER.showAlarmArray addObject:_model];
    }else{
        [SN_USER.showAlarmArray replaceObjectAtIndex:_model.orderIndex withObject:_model];
    }
    for (int i=0; i<SN_USER.showAlarmArray.count; i++) {
        [SN_USER.alarmArray replaceObjectAtIndex:i withObject:SN_USER.showAlarmArray[i]];
    }
    [SN_USER updateToDB];
    [SN_EveryDayModelHelp startSetAlarmClock];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)valueChange{
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
